<?php
/*
 * Database Manager Class
 * 
 * All the queries will be executed here, contains the whole database layer
 * 
 * @author : Jason Meulenhoff
 * @mail : mail@kacangboi.com
 * @web : http://kacangboi.com
 * @copyright : KacangNetworks 2010
 * 
 */
class databaseManager
{
	private $db; // holds the database commands;
	
	public function __construct()
	{
		debug::message("Database manager instance created","INFO");
		$this->db = new database();
	}
	
	/*Account Queries*/
	public function login($username,$password)
	{
		debug::message("Finding Account : $username with the password : $password","INFO");
		$this->db->execute("SELECT `username`, `password` FROM `".ACCOUNTS_TABLE."` WHERE `username` = '$username' && `password` = '".md5($password)."' && `portal` = '".PORTAL_NAME."'");
		return $this->db->rowsFound();
	}
	
	public function adminlogin($username,$password)
	{
		debug::message("Finding Admin Account : $username with the password : $password","INFO");
		$this->db->execute("SELECT `username`, `password`,`admin` FROM `".ACCOUNTS_TABLE."` WHERE `username` = '$username' && `password` = '".md5($password)."' && `admin` = '1' ");
		return $this->db->rowsFound();
	}
	
	public function registerUser($username,$password,$email)
	{
		if(!$this->userExists($username))
		{
			$this->db->execute("INSERT INTO `".ACCOUNTS_TABLE."` (`username`,`password`,`portal`) VALUES ('$username','".md5($password)."','".PORTAL_NAME."')"); 
			$message = '
 ---------------------------------------------
| Note this is an automatic generated message |
 ---------------------------------------------
			
 Portal : '.PORTAL_NAME.'
 Username : '.$username.'
 Password : '.$password.'
			
 Mail Creation Date : '.date("d-m-Y H:i:s").'
 ---------------------------------------------
			';
			
			mail($email, 'New user added to Portal : '.PORTAL_NAME.'',$message);
			
			echo '<span style="font-size: 13px;">Created account : '.$username.', details have been emailed to '.$email.'</span>';
		}
		else
		{
$message = '
 ---------------------------------------------
| Note this is an automatic generated message |
---------------------------------------------
			
The account could not be created 
			 
Reason : Account '.$username.' does already exists, check your previous emails.
			
Mail Creation Date : '.date("d-m-Y H:i:s").'
---------------------------------------------
';
			
			mail($email, 'Failed to add user to Portal : '.PORTAL_NAME.'',$message); 
			echo '<span style="font-size: 13px;"><img src="../images/error.png"/> cannot create account for '.$username.', details have been emailed to '.$email.'</span>';
		}
	}
	public function userExists($username)
	{
		$this->db->execute("SELECT * FROM `".ACCOUNTS_TABLE."` WHERE `username` = '$username'");
		return $this->db->rowsFound();
	}
	/*Session Queries*/
	public function countSessions()
	{
		$this->db->execute("SELECT count(*) AS `connections` FROM `".VIDEO_TABLE."`");
		$temp = $this->db->saveRow();
		return $temp['connections'];
	}
	
	public function setSession($username)
	{
		debug::message("Setting User Online : $username with the ip : ".$_SERVER['REMOTE_ADDR']."","INFO");
		$time = time() + SESSION_TIME; // 2 hours
		$this->db->execute("INSERT INTO `".ONLINE_TABLE."` (`ip`,`account`,`time`) VALUES ('".$_SERVER['REMOTE_ADDR']."','$username','$time')");
		return $this->db->rowsFound();
	}
	
	public function sessionExsists()
	{
		debug::message("Checking for sessions with the ip : ".$_SERVER['REMOTE_ADDR']."","INFO");
		$this->db->execute("SELECT `ip` FROM `".ONLINE_TABLE."` WHERE `ip` = '".$_SERVER['REMOTE_ADDR']."'");
		return $this->db->rowsFound();
			
	}
	public function expireSession()
	{
		$this->db->execute("DELETE FROM `".ONLINE_TABLE."` WHERE `ip` = '".$_SERVER['REMOTE_ADDR']."'");
	}
	
	public function sessionActive()
	{
		$this->db->execute("SELECT `time` FROM `".ONLINE_TABLE."` WHERE `ip` = '".$_SERVER['REMOTE_ADDR']."'");	
		$temp = $this->db->saveRow();
		return $temp['time'];
	}
	
	public function updateSession()
	{
		debug::message("Updating Session from ip : ".$_SERVER['REMOTE_ADDR']."","INFO");
		$time = time() + SESSION_TIME;
		$this->db->execute("UPDATE `".ONLINE_TABLE."` SET `time` = '$time' WHERE `ip` = '".$_SERVER['REMOTE_ADDR']."'");
	}
	
	/* Video Queries*/
	public function videoExists($name)
	{
		$this->db->execute("SELECT `name` FROM `".VIDEO_TABLE."` WHERE `name` = '$name'");
		return $this->db->rowsFound();
	}
	
	public function deleteVideo($name)
	{
		$this->db->execute("DELETE FROM `".VIDEO_TABLE."` WHERE `name` = '$name'");
	}
	
	public function insertNewVideos($name,$username,$portal,$date,$language)
	{
		$this->db->execute("INSERT INTO `".VIDEO_TABLE."` (`name`,`account`,`portal`,`date`,`language`) VALUES ('$name','$username','$portal','$date','$language')");
	}
	
	/*General Queries*/
	public function getLiveSettings()
	{
		$this->db->execute("SELECT * FROM `".SETTINGS_TABLE."` WHERE `setting` = 'language'");
		return $this->db->saveRow();	
	}
	
	public function updateLiveSettings($to)
	{
		$this->db->execute("UPDATE `".SETTINGS_TABLE."` SET `value` = '".strtoupper($to)."' WHERE `setting` = 'language'");
	}
	
	public function getLiveUrl()
	{
		$this->db->execute("SELECT * FROM `".SETTINGS_TABLE."` WHERE  `setting` = 'liveurl'");
		return $this->db->saveRow();
	}
	public function loadVideos($username)
	{
		$this->db->execute("SELECT * FROM `".VIDEO_TABLE."` WHERE `account` = '$username'");
		return $this->db->saveAllRows();	
	}
	
	public function readPublishingPoint($point)
	{
		switch($point)
		{
			case "ENGLISH" :
				$point = "pbenglish";
			break;
			case "DUTCH" : 
				$point = "pbdutch";
			break;
		}
		$this->db->execute("SELECT * FROM `".SETTINGS_TABLE."` WHERE `setting` = '$point'");
		return $this->db->saveRow();
	}
	
	public function savePublishingPoint($point,$value)
	{
		switch($point)
		{
			case "ENGLISH" :
				$point = 'pbenglish';
			break;
			case "DUTCH" :
				$point = 'pbdutch';
			break;
		}
		$this->db->execute("UPDATE `".SETTINGS_TABLE."` SET `value` = '$value' WHERE `setting` = '$point'");
	}
	
	/* Chat queries	 */
	public function saveChat($message,$author,$time)
	{
		$this->db->execute("INSERT INTO `".CHAT_TABLE."` (`message`,`author`,`time`) VALUES ('$message','$author','$time')");
	}
	
	public function clearChat()
	{
		$this->db->execute("TRUNCATE TABLE `".CHAT_TABLE."`");
	}
	
	public function readAllMessages()
	{
		$this->db->execute("SELECT * FROM `".CHAT_TABLE."` ORDER BY `time` DESC");
		return $this->db->saveAllRows();
	}
	
	public function saveUrl($value)
	{
		$this->db->execute("UPDATE `".SETTINGS_TABLE."` SET `value` = '$value' WHERE `setting` = 'liveurl'");
	}
	
	public function disableLive($value)
	{
		$this->db->execute("UPDATE `".SETTINGS_TABLE."` SET `value` = '$value' WHERE `setting` = 'disabled'");
	}
	
	public function getLive()
	{
		$this->db->execute("SELECT * FROM `".SETTINGS_TABLE."` WHERE `setting` = 'disabled'");
		$temp = $this->db->saveRow();
		return $temp['value'];
	}
}